<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Slideshow;
use app\api\model\SpeciallyClassify;
use app\api\model\ExamVenue as ExamVenueModel;
use app\api\model\UserAnswer;
use app\api\model\Topic;
/**
 * 竞赛专场
 */
class ExamVenue extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 竞赛专场
     *
     */
    public function index()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $page = empty($page) ? 1 : $page;
        $limit = empty($limit) ? 10 : $limit;
        $exam_venue = ExamVenueModel::field('id,img,title,sign_start,sign_end')->page($page,$limit)->select();  //专场
        $count = ExamVenueModel::count();  //专场总数
        if ($exam_venue) {
            foreach ($exam_venue as $key => $val) {
                $exam_venue[$key]['state'] = 0;
                if ($val['img']) {
                    $exam_venue[$key]['img'] = 'https://' . $_SERVER['HTTP_HOST'] . $val['img'];
                }
                if ($val['sign_start'] < time() && $val['sign_end'] > time()) {
                    $exam_venue[$key]['state'] = 1;
                }
                $exam_venue[$key]['sign_start'] = $val['sign_start'] * 1000;
                $exam_venue[$key]['sign_end'] = $val['sign_end'] * 1000;
            }
        }
        $data = [
            'count'=> $count,
            'exam_venue' =>$exam_venue,
            ];
        $this->success('请求成功',$data);
    }

    /**
     * 竞赛详情
     */
    public function details() {
        $id = $this->request->post('id');
        if (empty($id)) {
            $this->error('传参错误');
        }
        $userId = $this->auth->id;
        $exam_venue = ExamVenueModel::where('id',$id)->find();  
        if (!$exam_venue) {
            $this->error('竞赛不存在或已删除');
        } 
        if ($exam_venue['img']) {
            $exam_venue['img'] = 'https://' . $_SERVER['HTTP_HOST'] . $exam_venue['img'];
        }
        if ($exam_venue['sign_start']) {
            $time = $exam_venue['sign_start'];
            $exam_venue['sign_start'] = bcmul($time,1000);
            $exam_venue['sign_starts'] = date('Y-m-d H:i:s',$time);
        }
        if ($exam_venue['sign_end']) {
            $time = $exam_venue['sign_end'];
            $exam_venue['sign_end'] = bcmul($time,1000);
            $exam_venue['sign_ends'] =  date('Y-m-d H:i:s',$time);
        }
        if ($exam_venue['start_answer']) {
            $exam_venue['start_answer'] =  date('Y-m-d H:i:s',$exam_venue['start_answer']);
        }
        if ($exam_venue['end_answer']) {
            $exam_venue['end_answer'] =  date('Y-m-d H:i:s',$exam_venue['end_answer']);
        }
        $exam_venue['satae'] = 1;
        if ($userId) {
            $exam = UserAnswer::where(['uid'=>$userId,'id'=>$id])->find();
            if ($exam) {
                $exam_venue['satae'] = $exam['state'];
            }
        }
        $exam_venue['exam'] = UserAnswer::where(['uid'=>$userId,'de_id'=>$id,'type'=>2])->find();
        $this->success('请求成功',$exam_venue);

    }

    /**
     * 报名
     */
    public function sign_up() {
        $id = $this->request->post('id');
        if (empty($id)) {
            $this->error('传参错误');
        }
        $userId = $this->auth->id;
        $exam_venue = ExamVenueModel::where('id',$id)->find();  
        if (!$exam_venue) {
            $this->error('竞赛不存在或已删除');
        } 
        if ($exam_venue['sign_end'] < time()) {
            $this->error('报名时间已过');
        }
        $exam = UserAnswer::where(['uid'=>$userId,'de_id'=>$id,'type'=>2])->find();
        if ($exam) {
            return json(['code'=>2, 'msg'=>'已报名，不可重复参加']);
        }
        $data = [
            'uid' =>$userId,
            'de_id' => $id,
            'type' => 2,
            'state' =>2,
        ];
        $res = UserAnswer::insert($data);
        if ($res) {
            $this->success('报名成功');
        } else {
            $this->error('报名失败，稍后重试');
        }
    }

    
    /**
     * 取消报名
     */
    public function cancel() {
        $id = $this->request->post('id');
        $reason = $this->request->post('reason');
        if (empty($id)) {
            $this->error('传参错误');
        }
        $userId = $this->auth->id;
        $exam_venue = ExamVenueModel::where('id',$id)->find();  
        if (!$exam_venue) {
            $this->error('竞赛不存在或已删除');
        } 
        $data = [
            'state' => 0,
            'reason' => $reason,
        ];
        $res = UserAnswer::where(['uid'=>$userId,'de_id'=>$id,'type'=>2])->delete();
        if ($res) {
            $this->success('取消成功');
        } else {
            $this->error('取消失败，稍后重试');
        }
    }

    /**
     * 开始竞赛
     */
    public function begin() {
        $id = $this->request->post('id');
        if (empty($id)) {
            $this->error('传参错误');
        }
        $userId = $this->auth->id;
        $exam_venue = ExamVenueModel::where('id',$id)->find();  
        if (!$exam_venue) {
            $this->error('竞赛不存在或已删除');
        } 
        if(time() < $exam_venue['start_answer']){
             $this->error('还未到竞赛时间，请等待～');
        }

        if(time() > $exam_venue['end_answer']){
             $this->error('竞赛已结束！');
        }
        $res = UserAnswer::where(['uid'=>$userId,'de_id'=>$id,'type'=>2])->find();

        if (!$res) {
            $this->error('未报名，不可参与竞赛');
        } elseif ($res['state'] !=2) {
            $this->error('当前状态无法参与竞赛');
        }
        $to_ids = explode(',',$exam_venue['to_id']);
        $topic = Topic::where(['id'=>['in',$to_ids]])->select();
        foreach ($topic as $key => $val) {
            if ($val['type'] != 3) {
                $topic[$key]['answer'] = explode(',',$val['answer']);
            }
            if ($val['type'] == 2 || $val['type'] == 5) {
                $topic[$key]['correct_answer'] = explode(',',$val['correct_answer']);
            }
        }
        UserAnswer::where('id',$res['id'])->update(['start_answer'=>time()]);    //修改开始答题时间
        $this->success('获取成功',$topic);
        

    }

    /**
     * 提交答案
     */
    public function submit() {
        $id = $this->request->post('id');
        $select_answer = $this->request->post('select_answer');
        $gap_filling_answer = $this->request->post('gap_filling_answer');
        $duo_select_answer = $this->request->post('duo_select_answer');
        $estimate_answer = $this->request->post('estimate_answer');
        $case_answer = $this->request->post('case_answer');
        $userId = $this->auth->id;
        $exam_venue = ExamVenueModel::where('id',$id)->find();  
        if (!$exam_venue) {
            $this->error('竞赛不存在或已删除');
        }
        if ($exam_venue['end_answer'] < time()) {
            $this->error('答题时间已过');
        }
        $res = UserAnswer::where(['uid'=>$userId,'de_id'=>$id,'type'=>2])->find();
        if (!$res) {
            $this->error('当前状态，不可参与竞赛');
        }
        $end_answer = time();
        $answer_time = bcsub($end_answer,$res['start_answer']);
        $answer_time = bcdiv($answer_time,60);
        //计算分数
        
        $data = [
            'select_answer' => $select_answer,
            'gap_filling_answer' => $gap_filling_answer,
            'duo_select_answer' => $duo_select_answer,
            'estimate_answer' => $estimate_answer,
            'case_answer' => $case_answer,
            'end_answer' => $end_answer,
            'answer_time' => $answer_time,
            'topic' => $exam_venue['to_id'],
            'state' => 4,
        ];
        $ret = UserAnswer::where('id',$res['id'])->update($data);

        if ($ret) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败，稍后重试');
        }
    }




}
